package fiuba.descuentos;

import java.util.ArrayList;
import java.util.Vector;

import org.json.JSONException;
import org.json.JSONObject;

import Adapters.BenefitAdapter;
import Entities.Benefit;
import Listeners.CustomLocationListener;
import Service.Parameter;
import Service.WellKnownKeys;
import Service.WellKnownMethods;
import android.app.Activity;
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.GridView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class TextSearch extends ActivityBase {
	private ArrayList<Benefit> benefits;
	private GridView listview;
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.text_search);   
        
        Button button = (Button) findViewById(R.id.btn_search);
        button.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                bindBenefits();
            }
        });
    }
    
    private void bindBenefits(){
    	TextView search = (EditText) this.findViewById(R.id.txt_search);
		String text = search.getText().toString();
			
		Vector props = new Vector();
		Parameter textSearch = new Parameter(WellKnownKeys.TextKey,text);
		props.add(textSearch);
		
		Location loc = CustomLocationListener.getCurrentLocation();
		if (loc!=null){

			Parameter paramLatitud = new Parameter(WellKnownKeys.LatitudeKey, String.valueOf(loc.getLatitude()));
			Parameter paramLong = new Parameter(WellKnownKeys.LongitudKey, String.valueOf(loc.getLongitude()));			
						
			props.add(paramLatitud);
			props.add(paramLong);
		}
		
		callService(WellKnownMethods.GetBenefitsByText, props);
    }           

	@Override
	public void onServiceCompleted(JSONObject obj, String method) {
		if (WellKnownMethods.GetBenefitsByText.compareTo(method) == 0){
			onGetBenefitsCompleted(obj);
		}
	}

	private void onGetBenefitsCompleted(JSONObject obj) {
		boolean resultSuccess;
		hideLoading();
		try {
			resultSuccess = obj.getBoolean("success");
			if (resultSuccess) {
				this.benefits = Benefit.getFromJSON(obj);
				bindItems();
			}else{
				showErrorMessage(obj.getString("message"));
			}
		} catch (JSONException e) {
			e.printStackTrace();
		}				
	}
	
	private void bindItems(){
		if(benefits.isEmpty()){
			showErrorMessage("No se encontraron beneficios");
		}
		listview = (GridView) findViewById(R.id.list_benefits);		
		BenefitAdapter listAdapter = new BenefitAdapter(R.layout.benefit_column, benefits);
		listview.setAdapter(listAdapter);
		listview.setVisibility(View.VISIBLE);	
		listview.setOnItemClickListener(new OnItemClickListener() {
	        public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
	        	Intent intent = new Intent(parent.getContext(), BenefitDetail.class);
	        	intent.putExtra("benefit", benefits.get(position));
	        	startActivity(intent);
	        }
	    });
			
	}
}